package com.hrm.controller;


import com.hrm.entity.Job;
import com.hrm.service.hao.JobService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 职位管理
 *
 * @author 郝亚楠
 * @version 1.0
 * @date 2020.8.3 6:57
 */
@Controller
@RequestMapping(value = "/job")
public class JobController {
    @Autowired
    JobService jobService;
    Logger logger = Logger.getLogger(JobController.class);

    @GetMapping("/add")
    public String toAdd() {
        return "job/add";
    }

    @GetMapping("/list")
    public String list(@RequestParam(required = false) String name,
                       HttpServletRequest request) {
        List<Job> list = jobService.selectjoblist(name);
        request.setAttribute("list", list);
        request.setAttribute("name", name);
        return "job/list";
    }

    /**
     * 添加
     * @param job
     */

    @PostMapping(value = "/add")
    public void tjiao(Job job) {

        int count = jobService.addjob(job);
        if (count > 0) {
            System.out.println("添加成功！");
        }


    }

    /**
     * 删除
     * @param id
     */
    @GetMapping("delete")
    public void removeDept(@RequestParam Integer id) {
        int count = jobService.deletejobById(id);
        logger.info(count);
    }

    /**
     * 修改
     *
     * @param id
     * @return
     */
    @GetMapping("tiao")
    public ModelAndView tiao(Integer id) {
        ModelAndView view = new ModelAndView();
        view.addObject("id", id);
        view.setViewName("job/update");
        return view;
    }

    @PostMapping("update")
    public String update(Job job) {
        int count = jobService.update(job);
        logger.info(count);
        return "job/list";
    }
}
